<template>
  <div class="content-body" :style="borderStyle">
    <div class="user-content">
      <div class="user-image">
        <div>
          <el-upload
            :action="updateAvatarApi"
            :headers="headers"
            class="avatar-uploader"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :on-error="handleAvatarError"
            :before-upload="beforeAvatarUpload"
          >
            <img
              :key="keyNum"
              class="user-tx"
              :src="
                user.avatarName === null
                  ? tx
                  : baseURL.substring(0, baseURL.length - 1) + user.avatarName
              "
            >
            <!-- <el-button type="text">头像上传</el-button> -->
          </el-upload>
        </div>
      </div>

      <div class="user-info">
        <div class="user-content-body">
          <label>{{ setting.firstLabel }}：</label>{{ entity[setting.firstColumn] }}
        </div>
        <div class="user-content-body">
          <label>{{ setting.secondLabel }}：</label>{{ entity[setting.secondColumn] }}
        </div>
      </div>
      <div v-if="setting.data" class="user-tj">
        <div
          v-for="(item, index) in setting.data"
          :key="'tj-' + index"
          class="user-content"
        >
          <visiting-slot :column="item" @fetch-data="callBack" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import VisitingSlot from './slot/visitingSlot'
import { mapGetters } from 'vuex'
import setImage from '@/assets/images/user.png'
import { queryAllSystemData } from '@/api/portal/manager/analysis'
import { getToken } from '@/utils/auth'
export default {
  name: 'PcVisitingCardV1',
  components: { VisitingSlot },
  computed: {
    ...mapGetters(['user', 'updateAvatarApi']),
    themes() {
      return this.$store.getters.theme
    }
  },
  props: {
    temp: {
      type: Object,
      default() {
        return {}
      }
    },
    showHeader: {
      type: Boolean,
      default: true
    },
    layout: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      tx: setImage,
      baseURL: process.env.VUE_APP_BASE_API, // api 的 base_url
      loading: false,
      entity: {},
      setting: {},
      column: {},
      borderStyle: {},
      imageUrl: '',
      headers: {
        APEXEDU: getToken()
      },
      keyNum: 0
    }
  },
  watch: {
    layout: {
      handler(newData, oldData) {
        this.customStyle = newData
        this.titleStyle = {
          backgroundColor: newData.mrlb,
          opacity: newData.tmd
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    temp: {
      handler(newData, oldData) {
        if (this.showHeader === true) {
          const pzx = JSON.parse(newData.data.pzx)
          this.setting = pzx
          this.column = pzx.data
          this.cid = newData.data.id
          if (pzx.isRefresh !== undefined && pzx.isRefresh === true) {
            this.isRefresh = pzx.isRefresh
          }
          if (pzx.refreshTime !== undefined && pzx.refreshTime !== null) {
            this.refreshTime = pzx.refreshTime
          }
          this.initData()
        } else {
          const pzx = JSON.parse(newData.pzx)
          this.setting = pzx
          if (pzx.isRefresh !== undefined && pzx.isRefresh === true) {
            this.isRefresh = pzx.isRefresh
          }
          if (pzx.refreshTime !== undefined && pzx.refreshTime !== null) {
            this.refreshTime = pzx.refreshTime
          }
          this.column = pzx.data
          // 打印配置项是否有是否支持头像上传，然后再创建一个接口 方便给别人用 接口要求返回完整的图片地址，传递用户名就可以访问到图片
          this.cid = newData.id
          this.initData()
        }
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    themes: {
      handler(val, oldVal) {
        this.$nextTick(() => {
          this.borderStyle.borderRadius = val.bkdx
          if (this.temp.sfxsbk === true) {
            this.borderStyle.border = '1px solid ' + val.bkys
          }
          if (this.temp.bkyy === true) {
            this.borderStyle.boxShadow = '0 2px 4px rgba(0, 0, 0, .12)'
          }
          this.borderStyle.background = val.columnGroundColor
          this.borderStyle.color = val.fontColor
          this.borderStyle = Object.assign({}, this.borderStyle)
        })
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    }
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG =
        file.type === 'image/png' ||
        file.type === 'image/jpg' ||
        file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPG) {
        this.$message.error('只能上传png、jpg、jpeg图片!')
      }
      if (!isLt2M) {
        this.$message.error('上传上传图片大小不能超过2MB!')
      }
      return isJPG && isLt2M
    },
    isRefreshData() {
      this.keyNum++
    },
    handleAvatarSuccess(res) {
      if (res.url) {
        this.user.avatarName = res.url
        this.isRefreshData()
        var user = this.$store.getters.user
        user.avatarName = res.url
        this.$store.commit('SET_USER', user)
        // console.log(this.$store.getters.user.avatarName);
      }
    },
    handleAvatarError(err) {
      this.$message.error(err)
    },
    callBack(item) {
      if (undefined !== item) {
        item.menuId = this.temp.column.menu.id
        item.menuName = this.temp.column.menu.name
        item.columnName = this.temp.lmmc
        item.showHeader = true
        this.$emit('fetch-data', item)
      }
    },
    initData() {
      queryAllSystemData({ cid: this.cid })
        .then(res => {
          const { data } = res
          if (undefined !== data.dataList && data.dataList !== null) {
            this.dataList = data.dataList
            if (this.dataList.length > 0) {
              this.entity = this.dataList[0]
            }
          }
        })
        .catch(() => {
          this.dataList = []
        })
    }
  }
}
</script>

<style scoped>
.content-body {
  width: 100%;
  height: 100%;
  padding: 10px;
  box-sizing: border-box;
  overflow: hidden;
  background-color: #ffffff;
  border-radius: 4px;
}
.user-content {
  width: 100%;
  height: 100%;
  display: flex;
  flex-flow: row nowrap;
}
.user-image {
  width: 75px;
  flex: 0 1 auto;
  height: 100%;
  position: relative;
  text-align: center;
}
.user-info {
  flex: 1 1 auto;
  height: 100%;
  display: flex;
  flex-flow: column nowrap;
  align-content: center;
  padding: 15px;
  box-sizing: border-box;
}
.user-tx {
  width: 74px;
  height: 102px;
  top: 50%;
  margin-top: 5px;
  border-radius: 4px;
}
.user-tj {
  flex: 1 1 auto;
  display: flex;
  flex-flow: row nowrap;
  padding: 15px 0px;
  box-sizing: border-box;
}
.user-content {
  flex: 1 1 auto;
  text-align: center;
  cursor: pointer;
  padding: 5px 0px;
  box-sizing: border-box;
}
.user-content:not(:last-child) {
  border-right: 1px solid #eeeeee;
}
.user-content-body {
  flex: 1 1 auto;
  text-align: left;
  align-content: center;
  line-height: 3em;
}
</style>
